package com.yuan.model_lease.service.impl;

import com.yuan.model_common.dto.CommonQueryCriteria;
import com.yuan.model_lease.domain.LeaseRecords;
import com.yuan.model_lease.repository.LeaseRecordsDao;
import com.yuan.model_lease.service.LeaseRecordsService;
import com.yuan.model_lease.service.dto.LeaseRecordsDto;
import com.yuan.model_lease.service.dto.LeaseRecordsQueryCriteria;
import com.yuan.model_lease.service.mapstruct.LeaseRecordsMapper;
import lombok.RequiredArgsConstructor;
import me.zhengjie.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


@Service
@RequiredArgsConstructor
public class LeaseRecordsServiceImpl implements LeaseRecordsService {

    private final LeaseRecordsDao leaseRecordsDao;

    private final LeaseRecordsMapper leaseRecordsMapper;

    @Override
    public PageResult<LeaseRecordsDto> queryAll(CommonQueryCriteria criteria, Pageable pageable) {
        Page<LeaseRecords> page = leaseRecordsDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        for (LeaseRecords leaseRecords : page) {

        }
        return PageUtil.toPage(page.map(leaseRecordsMapper::toDto));
    }

    @Override
    public PageResult<LeaseRecordsDto> queryAllByType(LeaseRecordsQueryCriteria criteria, Pageable pageable) {
        Page<LeaseRecords> page = leaseRecordsDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(leaseRecordsMapper::toDto));
    }

    @Override
    public List<LeaseRecordsDto> queryAll(CommonQueryCriteria criteria) {
        List<LeaseRecords> users = leaseRecordsDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
        for (LeaseRecords user : users) {
            user.setSendUserName(leaseRecordsDao.findNiceNameByUserId(criteria.getUserId()));;
        }
        return leaseRecordsMapper.toDto(users);
    }

    @Override
    public void download(List<LeaseRecordsDto> queryAll, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (LeaseRecordsDto paymentBillDto : queryAll) {
            Map<String, Object> map = new LinkedHashMap<>();
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(LeaseRecords resources) {
        leaseRecordsDao.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(LeaseRecords resources) throws Exception {
        LeaseRecords leaseRecords = leaseRecordsDao.findById(resources.getRecordsId()).orElseGet(LeaseRecords::new);
        BeanUtils.copyProperties(resources, leaseRecords);
        leaseRecordsDao.save(leaseRecords);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer ids) {
        leaseRecordsDao.deleteById(ids);
    }
}
